package coollemon.kernel;

import java.util.ArrayList;

public class RelationTag implements Tag {

	private String name = "";
	private ArrayList<Pair<Contact, Contact>> relationList = new ArrayList<Pair<Contact, Contact>>();
	
	private RelationTag() {}
	
	public static RelationTag createRelationTag(String name) {
		RelationTag rt = TagManager.getRelationTag(name);
		if(rt == null) {
			rt = new RelationTag();
			rt.name = name;
			TagManager.addTag(rt);
		}
		return rt;
	}

	public ArrayList<Contact> getContacts(Contact con)
	{
		ArrayList<Contact> temp = new ArrayList<Contact>();
		for(int i = 0;i < relationList.size();i++)
		{
			if(relationList.get(i).getFirst().equals(con))
				temp.add(relationList.get(i).getSecond());
			else if(relationList.get(i).getSecond().equals(con))
				temp.add(relationList.get(i).getFirst());
		}
		return temp;
	}
	public ArrayList<Pair<Contact, Contact>> getRelationedPairs(Contact con) {
		ArrayList<Pair<Contact, Contact>> ret = new ArrayList<Pair<Contact, Contact>>();
		for(int i = 0; i < relationList.size(); i++) {
			if(relationList.get(i).contains(con))
				ret.add(relationList.get(i));
		}
		return ret;
	}
	public ArrayList<String> getContactNames(Contact con)
	{
		ArrayList<String> temp = new ArrayList<String>();
		for(int i = 0;i < relationList.size();i++)
		{
			if(relationList.get(i).getFirst().equals(con))
				temp.add(relationList.get(i).getSecond().getName());
			else if(relationList.get(i).getSecond().equals(con))
				temp.add(relationList.get(i).getFirst().getName());
		}
		return temp;
	}
	public String getName() {
		return this.name;
	}
	public ArrayList<Pair<Contact, Contact>> getContacts() {
		return relationList;
	}
	public ContactManager getContactManager() {
		ContactManager cm = new ContactManager();
		boolean dump = false;
		Contact temp = null;
		for(int i = 0; i < relationList.size(); i++) {
			dump = false;
			temp = relationList.get(i).getFirst();
			for(int j = 0; j < i; j++) {
				if(relationList.get(j).contains(temp)) {
					dump = true;
					break;
				}
			}
			if(!dump) 
				cm.add(temp);
			dump = false;
			temp = relationList.get(i).getSecond();
			for(int j = 0; j < i; j++) {
				if(relationList.get(j).contains(temp)) {
					dump = true;
					break;
				}
			}
			if(!dump) 
				cm.add(temp);
		}
		return cm;
	}
	@SuppressWarnings("unchecked")
	public void addContact(ContactBehave cb)
	throws ClassNotMatchException {
		if(!(cb instanceof Pair))
			throw new ClassNotMatchException();
		Pair<Contact, Contact> pair = (Pair<Contact, Contact>)cb;
		for(int i = 0; i < relationList.size(); i++) {
			if(relationList.get(i).containPair(pair))
				return;
		}
		relationList.add(pair);
	}
	public void add(Contact cona, Contact conb) {
		relationList.add(new Pair<Contact, Contact>(cona, conb));
	}
	@SuppressWarnings("unchecked")
	public void deleteContact(ContactBehave cb) 
	throws ClassNotMatchException {
		if(!(cb instanceof Pair))
			throw new ClassNotMatchException();
		Pair<Contact, Contact> pair = (Pair<Contact, Contact>)cb;
		for(int i = 0; i < relationList.size(); i++) {
			if(relationList.get(i).containPair(pair)) {
				relationList.remove(i);
				break;
			}
		}
	}
	public void delete(Contact cona, Contact conb) {
		try {
			deleteContact(new Pair<Contact, Contact>(cona, conb));
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
